//
//  ReportViewController.m
//  PureProject
//
//  Created by Tung Tran on 4/22/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ReportViewController.h"
#import "HeaderForReport.h"
#import "ReportCellViewController.h"
#import "JSON.h"


@interface ReportViewController ()

@end

@implementation ReportViewController

@synthesize reportTableView = _reportTableView;
@synthesize responseData = _responseData;
@synthesize jsonData = _jsonData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // Set title
    self.title = @"Data Report";
    
    // Home item
    UIBarButtonItem *homeItem = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(itemHomeIsPressed:)];
    self.navigationItem.leftBarButtonItem = homeItem;
    [homeItem release];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:  
                             [NSURL URLWithString:URL_GET_REPORT]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

#pragma mark - connection

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	NSLog(@"connectionDidFinishLoading");
    
    NSString *strData = [[NSString alloc] initWithData:self.responseData encoding:NSASCIIStringEncoding];
    NSLog(@"data = %@", strData);
    
    self.jsonData = [strData JSONValue];
    //NSLog(@"%@",self.jsonData.description);
    
    /*
    self.books = [[NSMutableArray alloc] initWithObjects: nil];
    for (NSInteger idB = 0; idB < [jsonData count]; idB++) {
        NSDictionary *book = [jsonData objectAtIndex:idB];
        
        NSString *idBook = [book objectForKey:@"id"];
        NSString *nameBook = [book objectForKey:@"title"];
        NSString *imgPath = [[book objectForKey:@"image"] stringByReplacingOccurrencesOfString:@"\\/" withString:@"\\"];
        NSString *filePath = [[book objectForKey:@"file"] stringByReplacingOccurrencesOfString:@"\\/" withString:@"\\"];
        
        Book *newBook = [[Book alloc] initWithIdBook:[idBook intValue] NameBook:nameBook ImageCoverPath:imgPath FilePath:filePath];
        [self.books addObject:newBook];
        [newBook release];
    }
    
    [self.tableView reloadData];
     */
    [self.reportTableView reloadData];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
	NSLog(@"connectionDidReceiveResponse");
	self.responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	NSLog(@"connectionDidReceiveData");
	[self.responseData appendData:data];
	
	NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
	NSLog(@"data = %@", str);
	//resultField.text = str;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
	NSLog(@"fail with error!!");
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ReportCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UIViewController *viewController = [[UIViewController alloc] initWithNibName:@"ReportCellViewController" bundle:nil];
        cell = (UITableViewCell *)viewController.view;
    }    
    
    UILabel *lb1 = (UILabel *)[cell viewWithTag:10];
    UILabel *lb2 = (UILabel *)[cell viewWithTag:11];
    UILabel *lb3 = (UILabel *)[cell viewWithTag:12];
    
    switch (indexPath.section) {
        case 0:
            if (indexPath.row == 0) {
                [lb1 setText:@"RISK UNITS FOR OPENING"];
                [lb2 setText:[self.jsonData objectForKey:@"worst_opening_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"worst_opening_count"]];
                NSLog(@"jsonData = %@", self.jsonData);                    
                //[lb3 setText:[self.jsonData objectForKey:@"worst_opening_count"]];
            }
            else if (indexPath.row == 1) {
                [lb1 setText:@"UNIT NOT OPEN"];
                [lb2 setText:[self.jsonData objectForKey:@"worst_notopen_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"worst_notopen_count"]];
            }
            else if (indexPath.row == 2) {
                [lb1 setText:@"UNIT NOT LEASED"];
                [lb2 setText:[self.jsonData objectForKey:@"worst_notlease_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"worst_notlease_count"]];
            }
            else if (indexPath.row == 3) {
                [lb1 setText:@"WORST CASE NOT OPEN"];
                [lb2 setText:[self.jsonData objectForKey:@"worst_nottoopen_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"worst_nottoopen_count"]];
            }
            else if (indexPath.row == 4) {
                [lb1 setText:@"WORST CASE TO OPEN (Mini & Small Shops)"];
                [lb2 setText:[self.jsonData objectForKey:@"worst_toopen_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"worst_toopen_count"]];
            }
            else if (indexPath.row == 5) {
                [lb1 setText:@"Occupancy of Mini-Anchor & Small Shops"];
                [lb2 setText:[self.jsonData objectForKey:@"worst_occupancy_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"worst_occupancy_count"]];
            }
            else if (indexPath.row == 6) {
                [lb1 setText:@"Worst Total Occupancy for Shopping Center"];
                [lb2 setText:[self.jsonData objectForKey:@"worst_total_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"worst_total_count"]];
            }
            
            break;
        
        case 1:
            if (indexPath.row == 0) {
                [lb1 setText:@"RISK UNITS FOR OPENING"];
                [lb2 setText:[self.jsonData objectForKey:@"likely_opening_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"likely_opening_count"]];
                NSLog(@"jsonData = %@", self.jsonData);                    
                //[lb3 setText:[self.jsonData objectForKey:@"worst_opening_count"]];
            }
            else if (indexPath.row == 1) {
                [lb1 setText:@"UNIT NOT OPEN"];
                [lb2 setText:[self.jsonData objectForKey:@"likely_notopen_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"likely_notopen_count"]];
            }
            else if (indexPath.row == 2) {
                [lb1 setText:@"UNIT NOT LEASED"];
                [lb2 setText:[self.jsonData objectForKey:@"likely_notlease_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"likely_notlease_count"]];
            }
            else if (indexPath.row == 3) {
                [lb1 setText:@"WORST CASE NOT OPEN"];
                [lb2 setText:[self.jsonData objectForKey:@"likely_nottoopen_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"likely_nottoopen_count"]];
            }
            else if (indexPath.row == 4) {
                [lb1 setText:@"WORST CASE TO OPEN (Mini & Small Shops)"];
                [lb2 setText:[self.jsonData objectForKey:@"likely_toopen_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"likely_toopen_count"]];
            }
            else if (indexPath.row == 5) {
                [lb1 setText:@"Occupancy of Mini-Anchor & Small Shops"];
                [lb2 setText:[self.jsonData objectForKey:@"likely_occupancy_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"likely_occupancy_count"]];
            }
            else if (indexPath.row == 6) {
                [lb1 setText:@"Worst Total Occupancy for Shopping Center"];
                [lb2 setText:[self.jsonData objectForKey:@"likely_total_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"likely_total_count"]];
            }

            break;
            
        case 2:
            if (indexPath.row == 0) {
                [lb1 setText:@"RISK UNITS FOR OPENING"];
                [lb2 setText:[self.jsonData objectForKey:@"best_opening_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"best_opening_count"]];
                NSLog(@"jsonData = %@", self.jsonData);                    
                //[lb3 setText:[self.jsonData objectForKey:@"worst_opening_count"]];
            }
            else if (indexPath.row == 1) {
                [lb1 setText:@"UNIT NOT OPEN"];
                [lb2 setText:[self.jsonData objectForKey:@"best_notopen_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"best_notopen_count"]];
            }
            else if (indexPath.row == 2) {
                [lb1 setText:@"UNIT NOT LEASED"];
                [lb2 setText:[self.jsonData objectForKey:@"best_notlease_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"best_notlease_count"]];
            }
            else if (indexPath.row == 3) {
                [lb1 setText:@"WORST CASE NOT OPEN"];
                [lb2 setText:[self.jsonData objectForKey:@"best_nottoopen_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"best_nottoopen_count"]];
            }
            else if (indexPath.row == 4) {
                [lb1 setText:@"WORST CASE TO OPEN (Mini & Small Shops)"];
                [lb2 setText:[self.jsonData objectForKey:@"best_toopen_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"best_toopen_count"]];
            }
            else if (indexPath.row == 5) {
                [lb1 setText:@"Occupancy of Mini-Anchor & Small Shops"];
                [lb2 setText:[self.jsonData objectForKey:@"best_occupancy_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"best_occupancy_count"]];
            }
            else if (indexPath.row == 6) {
                [lb1 setText:@"Worst Total Occupancy for Shopping Center"];
                [lb2 setText:[self.jsonData objectForKey:@"best_total_sqm"]];
                [lb3 setText:[self.jsonData objectForKey:@"best_total_count"]];
            }

            break;
            
        default:
            break;
    }
    
        
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
	return 22;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HeaderForReport *headerView = [[[HeaderForReport alloc] initWithNibName:@"HeaderForReport" bundle:nil] autorelease];
    UILabel *lb1 = (UILabel *)[headerView.view viewWithTag:10];
    UILabel *lb2 = (UILabel *)[headerView.view viewWithTag:11];
    UILabel *lb3 = (UILabel *)[headerView.view viewWithTag:12];
	if (section == 0){
		[lb1 setText:@"WORST CASE"];
        [lb2 setText:@"SQM"];
        [lb3 setText:@"ACCOUNT"];
	}
    else if (section == 1) {
        [lb1 setText:@"LIKELY CASE"];
        [lb2 setText:@"SQM"];
        [lb3 setText:@"ACCOUNT"];
    }
    else if (section == 2) {
        [lb1 setText:@"BEST CASE"];
        [lb2 setText:@"SQM"];
        [lb3 setText:@"COUNT"];
    }
    
    return headerView.view;
}

#pragma mark - Item Navigation Bar
- (void)itemHomeIsPressed:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
    
}
#pragma mark header

@end
